package com.anc.platform.bloomFilter;//package com.anc.redis.bloomFilter;
//
//
import com.anc.platform.model.platform.entity.ScmLogisticsCompany;
import com.anc.platform.model.platform.service.IScmLogisticsCompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Charsets;
import com.google.common.hash.Funnel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Configuration
public class BloomFilterConfig implements InitializingBean{

    @Autowired
    private IScmLogisticsCompanyService iScmLogisticsCompanyService;

    @Autowired

    private RedisTemplate template;

    @Bean
    public BloomFilterHelper<String> initBloomFilterHelper() {
        return new BloomFilterHelper<>((Funnel<String>) (from, into) -> into.putString(from, Charsets.UTF_8)
                .putString(from, Charsets.UTF_8), 1000000, 0.01);
    }

    /**
     * 布隆过滤器bean注入
     * @return
     */
    @Bean
    public BloomRedisService bloomRedisService(){
        BloomRedisService bloomRedisService = new BloomRedisService();
        bloomRedisService.setBloomFilterHelper(initBloomFilterHelper());
        bloomRedisService.setRedisTemplate(template);
        return bloomRedisService;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        QueryWrapper<ScmLogisticsCompany> wrapper=new QueryWrapper<>();
        List<ScmLogisticsCompany> list2 = iScmLogisticsCompanyService.list(wrapper);
        List<String> list = list2.stream().map(ScmLogisticsCompany::getId).collect(Collectors.toList());
        log.info("加载产品到布隆过滤器当中,size:{}",list.size());
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(item->{
                //LocalBloomFilter.put(item);
                bloomRedisService().addByBloomFilter(RedisKeyPrefixConst.PRODUCT_REDIS_BLOOM_FILTER,item);
            });
        }
    }
}
